package com.bangcommunity.bbframe.druid.filter.qtmonitor;

import com.bangcommunity.bbframe.common.utils.lang.SpiUtils;
import com.alibaba.druid.filter.AutoLoad;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.filter.stat.StatFilterContext;
import com.alibaba.druid.filter.stat.StatFilterContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by tanghc on 17/1/16.
 */
@AutoLoad
public class QtStatFilter extends StatFilter {
    private final static Logger logger = LoggerFactory.getLogger(QtStatFilter.class);

    public QtStatFilter() {
        init();
    }

    private void init() {
        autoLoadStatContextListener();
    }

    private void autoLoadStatContextListener() {
        List<StatFilterContextListener> druidAutoStatFilterContextListenerLoader = SpiUtils
                .listSpi(StatFilterContextListener.class);
        if (null != druidAutoStatFilterContextListenerLoader) {
            for (StatFilterContextListener autoStatFilterContextListener : druidAutoStatFilterContextListenerLoader) {
                if (logger.isDebugEnabled()) {
                    logger.debug("========== monitor ====  druid init  ==========");
                }
                AutoLoad autoLoad = autoStatFilterContextListener.getClass().getAnnotation(AutoLoad.class);
                if (autoLoad != null && autoLoad.value()) {
                    if (logger.isInfoEnabled()) {
                        logger.info("load autoStatFilterContextListener from spi :"
                                + autoStatFilterContextListener.getClass().getName());
                    }
                    StatFilterContext.getInstance().addContextListener(autoStatFilterContextListener);
                }
            }
        }
    }
}
